<template>
  <div class="app-container">
    <div slot="footer" class="dialog-footer" style="margin:20px 20px 20px 20px">
      <el-button type="warning" @click="$router.go(-1)">返 回</el-button>
    </div>
    <el-steps :active="active" finish-status="success" align-center>
      <el-step title="广告基本信息"></el-step>
      <el-step title="广告内容"></el-step>
    </el-steps>

    <el-form ref="form" :model="form" :rules="rules" label-width="120px">

      <el-row v-show="active == 1">
        <el-col :span="11">
          <el-form-item label="公司：" prop="enterpriseId">
            {{ name }}
          </el-form-item>
          <el-form-item style="width: 50%;" label="法人代表：" prop="legalRepresentative">
            {{ form.legalRepresentative }}
          </el-form-item>
          <el-form-item style="width: 50%;" label="联系电话：" prop="contactPhone">
            {{ form.contactPhone }}
          </el-form-item>
          <el-form-item style="width: 50%;" label="电子邮箱：" prop="email">
            {{ form.email }}
          </el-form-item>

          <el-form-item label="费用：" prop="cost" style="width: 40%">
            <span style="margin-right: 20px">{{ form.cost }}</span>
            <span>万元</span>
          </el-form-item>

          <el-form-item label="广告类型：" prop="type">
            <dict-tag :options="dict.type.village_advertisements_type" :value="form.type"/>
          </el-form-item>
          <el-form-item label="投放时间：" prop="startTime">
            {{ form.startTime }}
          </el-form-item>
          <el-form-item label="结束时间：" prop="endTime">
            {{ form.endTime }}
          </el-form-item>

          <el-form-item style="width: 60%;" label="推广目的：" prop="promotionGoal">
            {{ form.promotionGoal }}
          </el-form-item>

        </el-col>
        <el-col :span="2" style="height: 570px;display: flex;justify-content: center">
          <div style="height: 100%;width: 2px;background-color: #d9d9d9"></div>
        </el-col>
        <el-col :span="11">


          <el-form-item label="状态：" prop="status">
            <dict-tag :options="dict.type.village_activity_status" :value="form.status"/>

          </el-form-item>
          <el-form-item style="width: 50%;" label="官网地址" prop="officialWebsite">
            {{ form.officialWebsite }}
          </el-form-item>
          <el-form-item label="封面：" prop="contentImageUrl">
            <el-image
              style="height: 100px"
              :src="image"
            ></el-image>
          </el-form-item>
          <el-form-item label="轮播图片：" prop="carouselImages">
            <el-image
              v-for="(url, index) in images"
              :key="index" style="height: 100px;margin-right: 20px"
              :src="url"
            ></el-image>
          </el-form-item>

        </el-col>
      </el-row>

      <el-row v-show="active == 2">
        <el-col :span="24">


          <el-form-item style="width: 80%;" label="简介：" prop="introduction">
            {{ form.introduction }}
          </el-form-item>

          <el-form-item label="广告内容：" prop="content">
            <div style="margin-top: 15px;width: 100%;height: 500px;overflow-wrap: break-word;overflow:auto" v-html="form.content" ></div>
          </el-form-item>

        </el-col>
      </el-row>


      <div style="display: flex;justify-content: space-evenly">
        <el-button style="margin-top: 12px;width: 200px" type="primary" plain @click="lastStep"
                   :disabled="active === 1"
        >
          上一步
        </el-button>
        <el-button style="margin-top: 12px;width: 200px" type="primary" plain @click="next" :disabled="active === 2">
          下一步
        </el-button>
      </div>
    </el-form>

  </div>
</template>

<script>
import {
  listAdvertisements,
  getAdvertisements,
  delAdvertisements,
  addAdvertisements,
  updateAdvertisements
} from '@/api/village/advertisements'
import {
  listEnterprise,
  getEnterprise,
  delEnterprise,
  addEnterprise,
  updateEnterprise
} from '@/api/village/enterprise'

export default {
  name: 'Advertisements',
  dicts: ['village_activity_status', 'village_advertisements_type'],
  data() {
    return {
      images: '',
      image: '',
      name: '',
      active: 1,
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告信息管理表格数据
      advertisementsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        company: null,
        contactPhone: null,
        status: null,
        type: null,
        startTime: null,
        endTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()

    if (this.$route.query.id != null) {
      this.handleUpdate(this.$route.query)
    }

  },
  methods: {
    async getname(id) {
      await listEnterprise(this.queryParams).then(response => {
        this.options = response.rows.map(item => {
          return {
            value: item.id,
            label: item.name
          }
        })
      })
      const name = this.options.filter(item => item.value !== id)
      this.name = name[0].label
    },
    next() {
      if (this.active++ > 1) this.active = 1
    },
    lastStep() {
      if (this.active-- < 0) this.active = 2
    },
    /** 查询广告信息管理列表 */
    getList() {
      this.loading = true
      listAdvertisements(this.queryParams).then(response => {
        this.advertisementsList = response.rows
        this.total = response.total
        this.loading = false
      })
      listEnterprise(this.queryParams).then(response => {
        this.options = response.rows.map(item => {
          return {
            value: item.id,
            label: item.name
          }
        })
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.$router.push('/advertisements')
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        company: null,
        legalRepresentative: null,
        contactPhone: null,
        email: null,
        status: null,
        cost: null,
        promotionGoal: null,
        type: null,
        startTime: null,
        endTime: null,
        carouselImages: null,
        introduction: null,
        content: null,
        contentImageUrl: null,
        createdTime: null,
        updatedTime: null,
        createdBy: null,
        updatedBy: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加广告信息管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAdvertisements(id).then(response => {
        this.form = response.data
        this.getname(response.data.enterpriseId)
        this.image = process.env.VUE_APP_BASE_API + response.data.contentImageUrl
        this.images = response.data.carouselImages.split(',').map(item => process.env.VUE_APP_BASE_API + item)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAdvertisements(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.$router.push('/advertisements')
            })
          } else {
            addAdvertisements(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false

              this.$router.push('/advertisements')
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除广告信息管理编号为"' + ids + '"的数据项？').then(function() {
        return delAdvertisements(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('village/advertisements/export', {
        ...this.queryParams
      }, `advertisements_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>

</style>
